﻿//
// VKSL - Visual Knowledge in Second Life
// 2007(c) CC License by-nc-sa-2.5-it
//
// Authors:
//     JonnyBee Cioc    jonny@vulca.no
//     VisionRaymaker   vision.raymaker@vulca.no

// ===[ LICENSE ]=======================================

// License Creative Commons Attribution-Noncommercial-Share Alike 2.5 Italy
// http://creativecommons.org/licenses/by-nc-sa/2.5/it/

// ===[ UTILITY FUNCTIONS ]=======================================


// message linked public interface
debug(list _list)
{
    llMessageLinked(LINK_NUMBER, MESSAGELINKED_DEBUG, llGetScriptName()+SEPARATOR+llDumpList2String(_list,SEPARATOR));
}

sendML(integer messagelinked, string message)
{
    llMessageLinked(LINK_NUMBER, messagelinked, message, link_key);
}


// security check
// nobody can simply forge the correct message digest without knowing the map secret integer
// pass the check if the given md5 matches the computed one with the secret integer
integer check(string md5, string message)
{
    return md5==llMD5String(llGetSubString(message,32+separator_length, -1), map_secret);
}

// search for the param into the list
// return the paired value or default value

float findFloat(list params, integer param)
{
    integer index = llListFindList(params,[(string)param]);
    if(~index) return llList2Float(params,++index);
    else return 0.0;
}

integer findInt(list params, integer param)
{
    integer index = llListFindList(params,[(string)param]);
    if(~index) return llList2Integer(params,++index);
    else return -1;
}

key findKey(list params, integer param)
{
    integer index = llListFindList(params,[(string)param]);
    if(~index) return llList2Key(params,++index);
    else return NULL_KEY;
}

string findString(list params, integer param)
{
    integer index = llListFindList(params,[(string)param]);
    if(~index) return llList2String(params,++index);
    else return "";
}

vector findVector(list params, integer param)
{
    integer index = llListFindList(params,[(string)param]);
    if(~index) return llList2Vector(params,++index);
    else return ZERO_VECTOR;
}

string format(string str, list args)
{
    string pat;
    integer i=0;
    integer pos;
    integer plen;
    integer len = llGetListLength(args);
    for(;i<len;++i){
        pat = "%"+(string)i;
        plen= llStringLength(pat)-1;
        while((pos = llSubStringIndex(str,pat))>-1) {
            str = llInsertString(llDeleteSubString(str,pos,pos+plen),pos,llList2String(args,i));
        }
    }
    return str;
}

string numbered(list _list)
{
    string numberedlist;
    integer ii = llGetListLength(_list);
    integer i=0;
    for(;i<ii;++i)
        numberedlist += (string)(i+1)+". "+llList2String(_list, i)+"\n";
    return numberedlist;
}

string getSLUrl(string nameSim, vector localPos) {
    string slurl = "http://slurl.com/secondlife/";

    slurl += llEscapeURL(nameSim) + "/";
    slurl += (string)llRound(localPos.x) + "/";
    slurl += (string)llRound(localPos.y) + "/";
    slurl += (string)llRound(localPos.z) + "/";

    return slurl;
}

integer min(integer a, integer b) { if(a<b) return a; else return b; }



default
{

}